<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>请假信息管理系统 -- 添加课程</title>
    <script src="../../../js/jquery.min.js"></script>
    <link href="../../../css/courseManage/addCourse.css" rel="stylesheet">
    <link href="../../../css/common/common.css" rel="stylesheet">
</head>
<body>
<!-- 顶部弹出提示信息容器 -->
<div id="notification" class="notification">
    <span id="message"></span>
</div>
<!-- 头部 -->
<header th:insert="~{common/common::header}">
</header>
<!-- 左边 -->
<aside th:insert="~{common/common::aside}">

</aside>
<!-- 中间 -->
<main class="main">
    <form action="/course/addCourse" id="courseFrom">
        <h2 style="margin: 20px 30%;">新增课程信息</h2>
        <div class="form-group">
            <label for="courseId">课程编号</label>
            <input type="text" id="courseId" name="courseID">
        </div>
        <div class="form-group">
            <label for="classId">班级名称
                <select id="classId" name="classID" style="width: 400px;height: 40px;">
                    <option>请选择班级</option>
                </select>
            </label>

        </div>
        <div class="form-group">
            <label for="courseName">课程名称</label>
            <input type="text" id="courseName" name="courseName">
        </div>
        <div class="form-group">
            <label for="courseYear">课程学年
                <input type="text" id="courseYear" name="year">
            </label>
        </div>
        <div class="form-group">
            <label for="courseTerm">学期:
                <select id="courseTerm" name="term" style="width: 400px;height: 40px;">
                    <option label="上学期" value="0"></option>
                    <option label="下学期" value="1"></option>
                </select>
            </label>
        </div>
        <div class="form-group">
            <label for="courseHour">课程学时</label>
            <input type="number" id="courseHour" name="hour">
        </div>
        <input type="button" value="返回" onclick="goBack()">
        <input type="button" value="保存提交" onclick="addCourse()">
    </form>

</main>
</body>

<script th:inline="javascript" type="text/javascript">
    function goBack(){
        window.location.href = '/course/list?pageSize=7&pageNum=1'
    }
    var ids = [[${courseIds}]];
    console.log(ids)
    function addCourse(){
        var courseId = document.getElementById("courseId").value;
        var courseYear = document.getElementById("courseYear").value;
        var courseTerm = document.getElementById("courseTerm").value;
        if(courseId.length == 0 || courseId.length >30){
            document.getElementById("message").innerHTML = "课程编号非法，请重新输入";
            this.showNotification();
            return false;
        }
        if(courseYear.length >= 30){
            document.getElementById("message").innerHTML = "学年输入过长，请重新输入";
            this.showNotification();
            return false;
        }
        if(courseTerm.length >= 150){
            document.getElementById("message").innerHTML = "学期输入过长，请重新输入";
            this.showNotification();
            return false;
        }
        for(var id in ids){
            console.log(id)
            console.log(courseId)
            if(courseId === ids[id]){
                document.getElementById("message").innerHTML = "编号已存在，请重新输入";
                this.showNotification();
                return false;
            }
        }

        if (document.getElementById("classId").value == "请选择班级"){
            document.getElementById("message").innerHTML = "请选择班级";
            this.showNotification();
            return false;
        }
        document.getElementById("courseFrom").submit();
    }

    var classesList = [[${classesList}]];
    var className = []
    classesList.forEach(item => {
        className.push({
            "label" : item.className,
            "value" : item.classID
        })
    })
    var selectElement = document.getElementById("classId");
    className.forEach(option => {
        var optionElement = document.createElement("option");
        optionElement.value = option.value;
        optionElement.textContent = option.label;
        selectElement.appendChild(optionElement);
    })

    function showNotification() {
        const notification = document.getElementById("notification");
        notification.classList.add("show");

        setTimeout(function() {
            notification.classList.remove("show");
        }, 2000); // 3秒后自动消失
    }

    function outLogin(){
        location.href = '/outLogin'
    }
</script>

</html>